<?php
class CKecamatan extends DR_Controller
{
	function __Construct()
	{
		parent::__construct();
		$this->load->model("kecamatan");		
		$this->load->model("kabupaten");				
		$this->load->library("pagination");	
		
		$this->only_admin("admin/main/login");
	}
	
	private function redirect_if_null($id=null,$url=null)
	{
		if($id==null)
		{
			if($url!=null)
			{
				redirect($url);
			}
			
			redirect("admin/ckecamatan");
		}
	}
	
	function add()
	{
		$current_kecamatan = $this->kecamatan->get_blank_object();
		
		$data["form_caption"] = "Tambah Kecamatan ".$current_kecamatan->nama_kecamatan;
		$data["kecamatan"] = $current_kecamatan;
		$data["add_info"] = "";
		$data["action"] = site_url("/admin/ckecamatan/save");
		$data["btn_caption"] = "Simpan";
		
		$this->set_view("admin/header","admin/kecamatan/addnew",$data);
	}
	
	function save()
	{
		$nama = $this->read_post("nama");
		$id_provinsi = $this->read_post("id_provinsi");
		$kecamatan = $this->read_post("kecamatan");		
		$yayasan = $this->read_post("yayasan");
		$id = $this->read_post("id_kabupaten");
		
		$data = array("nama_kabupaten"=>$nama,
		              "id_provinsi"=>$id_provinsi,
					  "jumlah_kecamatan"=>$kecamatan,
					  "jumlah_yayasan"=>$yayasan
					  );
					  
		if($id!="0" && $id!="")
		{
			$this->kabupaten->update($data,$id);
		}
		else
		{
			//add
			$ada = $this->kabupaten->get_by_title($nama);
			if($ada==false)
			{
				$this->kabupaten->save($data);
			}
		}
		
		redirect("admin/ckabupaten");			
	}
	
	function edit($id=null)
	{
		$this->redirect_if_null($id);
		
		$current_kecamatan = $this->kecamatan->get_by_id($id);
		
		$data["form_caption"] = "Edit Kecamatan : ".$current_kecamatan ->nama_kecamatan;
		$data["kecamatan"] = $current_kecamatan;
		$data["add_info"] = "";
		$data["action"] = site_url("/admin/ckecamatan/save");
		$data["btn_caption"] = "Update";
		
		$this->set_view("admin/header","admin/kecamatan/addnew",$data);
	}
	
	function index($offset=null,$field=null,$value=null)
	{
		$filter=null;
		if($field!=null & $value!=null){
			$offset = $this->uri->segment(7); 
			$config['uri_segment']=7;					
			$filter = "$field like '%$value%'";
			$config['base_url'] = site_url("/admin/ckecamatan/index/0/$field/$value");
			$config['total_rows'] = $this->kecamatan->get_count($filter);			
			
			$data["filtered"] = true;
			
			//download filtered
			$this->session->set_flashdata("filter",$filter);			
			
		}
		else
		{
			$data["filtered"] = false;
			
			$offset = $this->uri->segment(4); 
			$config['uri_segment']=4;					
			$filter = array();
			$config['base_url'] = site_url('/admin/ckecamatan/index/');
			$config['total_rows'] = $this->kecamatan->get_count($filter);			
		}
				
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $paginator=$this->pagination->create_links();
		
		$data["offset"] = $offset;
		$this->kecamatan->set_default_order(array("nama_kecamatan"=>"asc"));		
		$data['result'] = $this->kecamatan->get_paged($config['per_page'],$offset,$filter);
		$data['total_page'] = $paginator;
		
		$data["config"] = $config;
		$this->set_view("admin/header",'admin/kecamatan/list',$data);
	}
}
?>